import os

from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from tortoise.contrib.fastapi import register_tortoise

from app.RAG.RAG_lx_jd import RAG
from app.chat.chat_wedsocket import connections
# from app.chat.chat_wedsocket import chat
from app.page.list_rz import dinhsi
from app.page.list_rz_wedSockert import list_rz_wedSockert
from app.user import dsf
# from app.page.xx import wietn_app
from tool.sessing import TORTOISE_ORM
from fastapi import APIRouter
from fastapi.responses import RedirectResponse

# 导入用户路由
from app.user.user_router import user_router

# 导入用户端页面路由
from app.page.page_user import page_user
from app.writer.writer import writer_router
from app.tueijian import tueijian_router

# 导入Coze AI路由
from app.AI_difi.coze import coze_router
from app.vip.vip import VIP
app = FastAPI(title="智能小说平台API", version="1.0.0")

# 配置CORS，允许前端访问
frontend_origins = {
    "http://localhost:5173",
    "http://127.0.0.1:5173",
    "http://127.0.0.1:5176",
    "http://localhost:8000",
    "http://127.0.0.1:8000",
    "http://localhost:5176",
    "http://6652cd12.r19.cpolar.top",
    "http://1d87115c.r19.cpolar.top",
    "http://233d381a.r19.cpolar.top",
    "https://6652cd12.r19.cpolar.top",
    "https://1d87115c.r19.cpolar.top",
    "https://233d381a.r19.cpolar.top",
}
extra_origin = os.getenv("FRONTEND_BASE_URL")
if extra_origin:
    frontend_origins.add(extra_origin.rstrip("/"))

extra_origin_list = os.getenv("FRONTEND_EXTRA_ORIGINS")
if extra_origin_list:
    for origin in extra_origin_list.split(","):
        if origin.strip():
            frontend_origins.add(origin.strip().rstrip("/"))

app.add_middleware(
    CORSMiddleware,
    allow_origins=list(frontend_origins),
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)


@app.get("/")
async def root():
    return {"message": "Hloa 吧的后端启动成功"}


@app.get("/hello/{name}")
async def say_hello(name: str):
    return {"message": f"Hello {name}"}


# # 用户定时任务
# app.include_router(dinhsi,prefix="/page",tags=["身份证定时任务"])

#rag查询
app.include_router(RAG, prefix="/RAG", tags=["rag查询"])

app.include_router(list_rz_wedSockert, prefix="/pahe", tags=["身份证使用全双工通信"])

# 注册用户路由（包含钉钉登录功能）
app.include_router(user_router, prefix="/user", tags=["用户"])

# 注册用户端页面路由
app.include_router(page_user, prefix="/page", tags=["用户端"])
# 注册作者私有路由
app.include_router(writer_router, prefix="/writer", tags=["作者"])

# 注册Coze智能体路由
app.include_router(coze_router)

# 智能推荐
app.include_router(tueijian_router)
app.include_router(VIP, prefix="/vip", tags=["vip支付"])


# 对话
# app.include_router(chat, prefix="/chat", tags=["对话"])

from app.user.dsf_gc import dsf

app.include_router(dsf, prefix="/user", tags=["第三方登录"])

app.include_router(RAG, prefix="/RAG", tags=["rag查询"])

app.include_router(connections, prefix="/connections", tags=["客服"])

# 注册所有API路由
api_router = APIRouter(prefix="/api")
app.include_router(api_router)

import uvicorn

# 注册Tortoise ORM
register_tortoise(
    app,
    config=TORTOISE_ORM,
    generate_schemas=True,
)

if __name__ == "__main__":
    uvicorn.run(app, host="0.0.0.0", port=8000)
